package com.example.project.service;

import com.example.project.dao.AggregationInterface;
import com.example.project.dao.RolesInterface;
import com.example.project.dao.UserInterface;
import com.example.project.entity.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class AggregationService {
    @Resource
    AggregationInterface aggregationInterface;
    @Resource
    UserInterface userInterface;
    @Resource
    RolesInterface rolesInterface;
    //库存汇总表单
    public ResponseInfo aggregationtable(Aggregation aggregation, RequestInto params) {
        params.setStart((params.getPage()-1)*params.getSize());
        List<Aggregation> list = aggregationInterface.aggregationtable(aggregation,params);
        long Counttotal=aggregationInterface.queryTableCount(aggregation,params);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("total", Counttotal);
        return  ResponseInfo.success(map);
    }
    //获取角色
    public ResponseInfo aggregationrole(String token) {
        User tokenUser = userInterface.queryUser(token);
        List<Roles> tokenRolesflags = rolesInterface.queryRolesflags(tokenUser);
        Map<String, Object> map = new HashMap<>();
        map.put("userrows", tokenUser);
        map.put("rows", tokenRolesflags);
        return ResponseInfo.success(map);
    }
    //获取管理员
    public ResponseInfo getadmin() {
        List<User> list = aggregationInterface.getadmin();
        long adminCount=aggregationInterface.adminCount();
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("count", adminCount);
        return ResponseInfo.success(map);
    }
    //添加到库存汇总
    public ResponseInfo addprodectorder(Aggregation aggregation) {
        aggregationInterface.addprodectorder(aggregation);
        return ResponseInfo.success(null);
    }
    //出库单表单
    public ResponseInfo outboundtable(String id, RequestInto params) {
        params.setStart((params.getPage()-1)*params.getSize());
        List<Aggregation> list = aggregationInterface.outboundtable(id,params);
        long Counttotal=aggregationInterface.queryoutboundTableCount(id,params);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("Counttotal", Counttotal);
        return  ResponseInfo.success(map);
    }
    //撤销删除出库表数据
    public ResponseInfo deloutbound(String id) {
        aggregationInterface.deloutbound(id);
        return ResponseInfo.success(null);
    }
    //修改表单数据
    public ResponseInfo editoutbound(Aggregation aggregation) {
        System.out.println("22222");
        aggregationInterface.editoutbound(aggregation);
        System.out.println("222");
        return ResponseInfo.success(null);
    }
    //入库单表单
    public ResponseInfo warehousingtable(String id, RequestInto params) {
        params.setStart((params.getPage()-1)*params.getSize());
        List<Aggregation> list = aggregationInterface.warehousingtable(id,params);
        long Counttotal=aggregationInterface.querywarehousingTableCount(id,params);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("Counttotal", Counttotal);
        return  ResponseInfo.success(map);
    }
    //撤销删除入库表数据
    public ResponseInfo delwarehousing(String id) {
        aggregationInterface.delwarehousing(id);
        return ResponseInfo.success(null);
    }
    //修改入库表单数据
    public ResponseInfo editwarehousing(Aggregation aggregation) {
        aggregationInterface.editwarehousing(aggregation);
        return ResponseInfo.success(null);
    }
    //单据审核
//单据表单
    public ResponseInfo documenttable(Aggregation aggregation, RequestInto params) {
        params.setStart((params.getPage()-1)*params.getSize());
        List<Aggregation> list = aggregationInterface.documenttable(aggregation,params);
        long Counttotal=aggregationInterface.querydocumentTableCount(aggregation,params);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("Counttotal", Counttotal);
        return  ResponseInfo.success(map);
    }
    //通过入库单
    public ResponseInfo passproduct(Aggregation aggregation, Product product) {
        long Counttypeid=aggregationInterface.querytypeidCount(aggregation,product);

        if (Counttypeid>0){
            aggregationInterface.editprodect(aggregation,product);
        }
        if (Counttypeid==0){
            aggregationInterface.addprodect(aggregation,product);
            aggregationInterface.addendprodect(aggregation);
        }

        return ResponseInfo.success(null);
    }
    //出库单
    public ResponseInfo rejectproduct(Aggregation aggregation, Product product) {
        long Counttypeid=aggregationInterface.querytypeidCount(aggregation, product);
//        库存表要判断，若该类别的产品不存在
        if (Counttypeid==0){
            aggregationInterface.editreason(aggregation);
        }
//        若产品存在，判断销售数量与库存数量
        if (Counttypeid>0){
            long Counttotal=aggregationInterface.totalCount(product);
            long Countnumber=aggregationInterface.numberCount(aggregation);
            System.out.println("Counttotal"+Counttotal+"Countnumber"+Countnumber);
//            若销售数量与库存数量相同
            if (Counttotal==Countnumber){
                aggregationInterface.delproduct(product);
                aggregationInterface.addendprodect(aggregation);
            }
//            库存不足，则直接驳回
            else if(Counttotal<Countnumber){
                aggregationInterface.editreason(aggregation);
            }
//            库存充足，要减去库存字段相应数量
            else if(Counttotal>Countnumber){
                aggregationInterface.edittotal(aggregation,product);
            }
            else {
                return ResponseInfo.fail(203,"产品有误");
            }
        }
        return ResponseInfo.success(null);
    }

    public ResponseInfo rejectreasonproduct(Aggregation aggregation) {
        aggregationInterface.rejectreason(aggregation);
        return ResponseInfo.success(null);
    }
}
